﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class SubPolicyUpdate : System.Web.UI.Page
    {
        private int editid = 0, delid = 0, page = 1, mainId = 0, subId = 0, SubjectId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterRole"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["mainId"] != null)
            {
                mainId = Convert.ToInt32(Request.QueryString["mainId"].Trim());
            }
            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["subId"] != null)
            {
                subId = Convert.ToInt32(Request.QueryString["subId"].Trim());
            }
            if (Request.QueryString["SubjectId"] != null)
            {
                SubjectId = Convert.ToInt32(Request.QueryString["SubjectId"].Trim());
            }
            
            if (!IsPostBack)
            {
                if (delid > 0)
                {
                    TestPaperPolicySub.Delete(delid);
                    Response.Redirect("SubPolicy.aspx?id=" + mainId.ToString() + "&page=" + page.ToString() + "&subId=" + subId.ToString() + "&SubjectId=" + SubjectId.ToString());
                    return;
                }
                BindingQuestionType();
                if (editid > 0)
                {
                    TestPaperPolicySubInfo sub = TestPaperPolicySub.GetT1(editid);
                    this.tbName.Text = sub.Name;
                    this.tbOrder.Text = sub.Order.ToString();
                   
                }
            }
        }

        void BindingQuestionType()
        {
            IList<QuesTypeInfo> list = QuesType.GetQuesTypeInfos(0, int.MaxValue);
            this.drpQuestionType.DataTextField = "Name";
            this.drpQuestionType.DataValueField = "Id";
            this.drpQuestionType.DataSource = list;
            this.drpQuestionType.DataBind();
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Trim() == "")
            {
                Utility.ShowJsAlert("请填写名称", this);
                return;
            }
            else
            {
                TestPaperPolicySubInfo sub = new TestPaperPolicySubInfo();
                sub.Name = this.tbName.Text;
                sub.Order = int.Parse(this.tbOrder.Text);
               
                if (editid > 0)
                {
                    sub.Id = editid;
                    TestPaperPolicySub.Edit(sub);
                    //Utility.ReloadTopPage(this);
                    this.ClientScript.RegisterStartupScript(this.GetType(), "key", "Refresh();", true);
                }
                else
                {
                    sub.PolicyMainId = mainId;
                    sub.CreateDate = DateTime.Now;
                    string content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                    "<question type=\"" + this.drpQuestionType.SelectedValue + "\" >" +
                     "</question>";
                    sub.Content = content;

                    TestPaperPolicySub.Add(sub);
                    this.ClientScript.RegisterStartupScript(this.GetType(), "key", "Refresh();", true);
                    //Utility.ReloadTopPage(this);
                    //this.Response.Redirect("SubPolicy.aspx?id="+mainId.ToString()+"&page="+page.ToString());
                }
            }
        }
    }
}
